
## Eunji Kim and Cindy Kam
## Othering in Everyday Life: Anti-Chinese Bias in the COVID-19 Pandemic
## Public Opinion Quarterly  
## Codes to replicate all figures and tables in the manuscript 


rm(list=ls())

## load libraries 

library(tm)
library(tidyverse)
library(ggplot2)
library(dotwhisker)
library(glue)
library(stringr)
library(xts)
library(lubridate)
library(plyr)
library(ggpubr)
library(dplyr)
library(fixest)
library(ggeffects)
library(extrafont)
library(lfe)
library(estimatr)
library(ggeffects)
library(ggeasy)

## load needed data before running the analyses 

#save(all1, all2, all3, all4, all5, all6, figure1, 
#     file = "poq_kimkam_main.RData")

load("poq_kimkam_main.RData")

## Main Manuscript Table 1 The Impact of the Pandemic on Chinese Restaurant Ratings

# Jan 2020 - April 2020
c1  <- feols(rating ~ chinese_vs_american*prepost   | city + elitestatus,cluster = ~business.id,data = all1)

# Jan 2020 - May 2020
c2  <- feols(rating ~ chinese_vs_american*prepost   | city + elitestatus,cluster = ~business.id,data = all2)

# Jan 2020 - June 2020
c3  <- feols(rating ~ chinese_vs_american*prepost   | city + elitestatus,cluster = ~business.id,data = all3)

# Jan 2020 - July 2020
c4  <- feols(rating ~ chinese_vs_american*prepost   | city + elitestatus,cluster = ~business.id,data = all4)

# Jan 2020 - August 2020
c5  <- feols(rating ~ chinese_vs_american*prepost   | city + elitestatus,cluster = ~business.id,data = all5)

# Jan 2020 - September 2020 
c6  <- feols(rating ~ chinese_vs_american*prepost   | city + elitestatus,cluster = ~business.id,data = all6)


etable(c1, c2, c3, c4, c5, c6, digits = 3,label = c("tab:yelp"),tex = T, depvar = F, title = "The Impact of Pandemic on Chinese Restaurants (All)",
       file = "Kim and Kam_Table 1.tex",replace=T)



## Main Manuscript: Figure 1 


library(zoo)


plot20<-  ggplot(figure1, aes(x=date, y=mean, colour=chinese, group=prepost_chinese),
                 scale_fill_manual(breaks = c("Chinese", "American"), values =c("#8a0002", "black"))) +
  geom_jitter( size=1, aes(colour=chinese, shape=chinese), alpha=0.8) + xlab('') + ylab('Ratings (Rolling 7-Day Average)') +
  scale_shape_manual(values=c(21, 19))+
  scale_color_manual(values = c("Chinese" = "#8a0002", "American" = "black")) +
  geom_smooth(aes(x=date, y=mean, fill=chinese), method=loess) + 
  scale_fill_manual(values = c("Chinese" = "#8a0002", "American" = "black")) + 
  scale_x_date(date_breaks = "months" , date_labels = "%b'%y") +
  geom_vline(xintercept = as.numeric(ymd("2020-03-13")), linetype="dotted", 
             color = "black", size=0.5) +
  theme_classic()+
  theme(legend.position = "none",  
        legend.title = element_blank(),
        plot.title = element_text(color="black", size = 15, face = "bold"),
        axis.title.x = element_text(color="black", size=14, face="bold"),
        axis.text.y = element_text(color= "black", size=11, face="bold"),
        axis.text.x = element_text(color= "black", size=11, face="bold"),
        axis.title.y = element_text(color="black", size=14, face="bold")) 

plot20 <- plot20 + 
  ggplot2::annotate("text", x = ymd("2020-05-01"), y = 4.5, fontface=2, size=3.5, 
                    label = "President Trump Declares \n State of Emergency for COVID-19") + 
  ggplot2::annotate("label", x = ymd("2020-05-01"), y = 4, label = "American Restaurants", colour = "#2E383D", fontface=2, size=3) + 
  ggplot2::annotate("label", x = ymd("2020-05-16"), y = 3.63, label = "Chinese Restaurants", colour = "#8a0002", fontface=2, size=3)


plot20 

ggsave("Kim and Kam_Figure1.jpeg",width=8,height=7, dpi=600)


## Main Manuscript: Figure 2 

j  <- feols(rating ~ japanese_vs_american*prepost | city + elitestatus,cluster = ~business.id,data = all1)
k  <- feols(rating ~ korean_vs_american*prepost | city  + elitestatus,cluster = ~business.id, data = all1)
v <- feols(rating ~ vietnam_vs_american*prepost | city + elitestatus,cluster = ~business.id, data = all1)
i <- feols(rating ~ indian_vs_american*prepost | city + elitestatus, cluster = ~business.id,data = all1)
t  <- feols(rating ~ thai_vs_american*prepost | city  + elitestatus,cluster = ~business.id, data = all1)


library(ggeasy)

jm <- summary(j)
j1 <- data.frame(Variable = c("Japanese","Lockdown",  "Japanese x Lockdown"),
                 Coefficient = c(jm$coefficients[1], jm$coefficients[2], jm$coefficients[3]), 
                 SE = c(jm$se[1], jm$se[2], jm$se[3]))

j1$Variable <- factor(j1$Variable, levels = c("Japanese","Lockdown",  "Japanese x Lockdown"))
levels(j1$Variable ) <- gsub(" ", "\n", levels(j1$Variable))


interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier

jp  <- ggplot(j1,aes(x = Variable, y = Coefficient)) + 
  geom_linerange(aes(ymin=Coefficient - SE*interval2, ymax=Coefficient + SE*interval2), width=0, size =2, 
                 alpha = 0.5, position=position_dodge(1)) + 
  #geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_point(colour = "black", size = 2, stroke = 1)  + 
  ggtitle("Japanese") +  
  ylab("") + xlab("") +  theme_minimal() +  
  theme(axis.title.x = element_text(color="black", size=14, face="bold"),
        axis.text.y = element_text(color= "black", size=12),
        axis.text.x = element_text(color= "black", size=12),
        axis.title.y = element_text(color="black", size=14, face="bold"),
        plot.title = element_text(size = 12, face="bold")) + 
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major = element_line(color = "gray50", size = 0.1),
        panel.grid.major.x = element_blank(),
        panel.background = element_blank()) +   ylim(c(-0.2,0.5))  +
  geom_hline(yintercept=0, linetype="dashed", 
             color = "#8a0002", size=0.5) +
  ggeasy::easy_center_title() + 
  theme(plot.title = element_text(size = 20, face = "bold"))



km <- summary(k)

k1 <- data.frame(Variable = c("Korean","Lockdown",  "Korean x Lockdown"),
                 Coefficient = c(km$coefficients[1], 
                                 km$coefficients[2], 
                                 km$coefficients[3]), 
                 SE = c(km$se[1], 
                        km$se[2], 
                        km$se[3]))

k1$Variable <- factor(k1$Variable, levels = c("Korean","Lockdown",  "Korean x Lockdown"))
levels(k1$Variable ) <- gsub(" ", "\n", levels(k1$Variable))


interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier

kp  <- ggplot(k1,aes(x = Variable, y = Coefficient)) + 
  geom_linerange(aes(ymin=Coefficient - SE*interval2, ymax=Coefficient + SE*interval2), width=0, size =2, 
                 alpha = 0.5, position=position_dodge(1)) + 
  geom_point(colour = "black", size = 2, stroke = 1)  + 
  ggtitle("Korean") +
  ylab("") + xlab("") +  theme_minimal() +  
  theme(axis.title.x = element_text(color="black", size=14, face="bold"),
        axis.text.y = element_blank(),
        axis.text.x = element_text(color= "black", size=12),
        axis.title.y = element_text(color="black", size=14, face="bold"),
        plot.title = element_text(size = 12, face="bold")) + 
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major = element_line(color = "gray50", size = 0.1),
        panel.grid.major.x = element_blank(),
        panel.background = element_blank()) +     ylim(c(-0.2,0.5))  +
  geom_hline(yintercept=0, linetype="dashed", 
             color = "#8a0002", size=0.5) + ggeasy::easy_center_title() + 
  theme(plot.title = element_text(size = 20, face = "bold"))


vm <- summary(v)


v1 <- data.frame(Variable = c("Vietnamese","Lockdown",  "Vietnamese x Lockdown"),
                 Coefficient = c(vm$coefficients[1], 
                                 vm$coefficients[2], 
                                 vm$coefficients[3]), 
                 SE = c(vm$se[1], 
                        vm$se[2], 
                        vm$se[3]))

v1$Variable <- factor(v1$Variable, levels = c("Vietnamese","Lockdown",  "Vietnamese x Lockdown"))
levels(v1$Variable ) <- gsub(" ", "\n", levels(v1$Variable))


interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier

vp  <- ggplot(v1,aes(x = Variable, y = Coefficient)) + 
  geom_linerange(aes(ymin=Coefficient - SE*interval2, ymax=Coefficient + SE*interval2), width=0, size =2, 
                 alpha = 0.5, position=position_dodge(1)) + 
  #geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_point(colour = "black", size = 2, stroke = 1)  + 
  ggtitle("Vietnamese") +
  ylab("") + xlab("") +  theme_minimal() +  
  theme(axis.title.x = element_text(color="black", size=14, face="bold"),
        axis.text.y = element_blank(),
        axis.text.x = element_text(color= "black", size=12),
        axis.title.y = element_text(color="black", size=14, face="bold"),
        plot.title = element_text(size = 12, face="bold")) + 
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major = element_line(color = "gray50", size = 0.1),
        panel.grid.major.x = element_blank(),
        panel.background = element_blank()) +    ylim(c(-0.2,0.5))  +
  geom_hline(yintercept=0, linetype="dashed", 
             color = "#8a0002", size=0.5) + ggeasy::easy_center_title() + 
  theme(plot.title = element_text(size = 20, face = "bold"))


tm <- summary(t)


t1 <- data.frame(Variable = c("Thai","Lockdown",  "Thai x Lockdown"),
                 Coefficient = c(tm$coefficients[1], 
                                 tm$coefficients[2], 
                                 tm$coefficients[3]), 
                 SE = c(tm$se[1], 
                        tm$se[2], 
                        tm$se[3]))

t1$Variable <- factor(t1$Variable, levels = c("Thai","Lockdown",  "Thai x Lockdown"))
levels(t1$Variable ) <- gsub(" ", "\n", levels(t1$Variable))

interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier

tp  <- ggplot(t1,aes(x = Variable, y = Coefficient)) + 
  geom_linerange(aes(ymin=Coefficient - SE*interval2, ymax=Coefficient + SE*interval2), width=0, size =2, 
                 alpha = 0.5, position=position_dodge(1)) + 
  #geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_point(colour = "black", size = 2, stroke = 1)  + 
  ggtitle("Thai") +
  ylab("") + xlab("") +  theme_minimal() +  
  theme(axis.title.x = element_text(color="black", size=14, face="bold"),
        axis.text.y = element_blank(),
        axis.text.x = element_text(color= "black", size=12),
        axis.title.y = element_text(color="black", size=14, face="bold"),
        plot.title = element_text(size = 12, face="bold")) + 
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major = element_line(color = "gray50", size = 0.1),
        panel.grid.major.x = element_blank(),
        panel.background = element_blank()) + ylim(c(-0.2,0.5))  +
  geom_hline(yintercept=0, linetype="dashed", 
             color = "#8a0002", size=0.5) + ggeasy::easy_center_title() + 
  theme(plot.title = element_text(size = 20, face = "bold"))


im <- summary(i)


i1 <- data.frame(Variable = c("Indian","Lockdown",  "Indian x Lockdown"),
                 Coefficient = c(im$coefficients[1], 
                                 im$coefficients[2], 
                                 im$coefficients[3]), 
                 SE = c(im$se[1], 
                        im$se[2], 
                        im$se[3]))

i1$Variable <- factor(i1$Variable, levels = c("Indian","Lockdown",  "Indian x Lockdown"))
levels(i1$Variable ) <- gsub(" ", "\n", levels(i1$Variable))


interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier

ip  <- ggplot(i1,aes(x = Variable, y = Coefficient)) + 
  geom_linerange(aes(ymin=Coefficient - SE*interval2, ymax=Coefficient + SE*interval2), width=0, size =2, 
                 alpha = 0.5, position=position_dodge(1)) + 
  #geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_point(colour = "black", size = 2, stroke = 1)  + 
  ggtitle("Indian") +
  ylab("") +xlab("") +  theme_minimal() +  
  theme(axis.title.x = element_text(color="black", size=14, face="bold"),
        axis.text.y = element_blank(),
        axis.text.x = element_text(color= "black", size=12),
        axis.title.y = element_text(color="black", size=14, face="bold"),
        plot.title = element_text(size = 20, face="bold")) + 
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major = element_line(color = "gray50", size = 0.1),
        panel.grid.major.x = element_blank(),
        panel.background = element_blank()) +     ylim(c(-0.2,0.5))  +
  geom_hline(yintercept=0, linetype="dashed", 
             color = "#8a0002", size=0.5) + ggeasy::easy_center_title() + 
  theme(plot.title = element_text(size = 20, face = "bold"))


ethnic <- ggarrange(jp, kp, vp, tp, ip, nrow=1, widths = c(1.2, 1, 1,1, 1))



jpeg("Kim and Kam_Figure2.jpeg", width=17, height=6, units='in', res=1000)

ethnic


dev.off()
